
(function () {
  'use strict';

  angular.module('BlurAdmin.pages.staffManage.companyList')
      .controller('CompanyListPageCtrl', CompanyListPageCtrl);

  /** @ngInject */
  function CompanyListPageCtrl($scope, $state, hotelManageService, $uibModal, PAGE_SIZE, toastr, SweetAlert) {
    $scope.PAGE_SIZE = PAGE_SIZE;
    $scope.findWhereFunc = _.findWhere;

    $scope.callServer = function (tableState, ctrl) {
      if ( !$scope.stCtrl && ctrl ) {
        $scope.stCtrl = ctrl;
      }
      if( !tableState && $scope.stCtrl ) {
        $scope.stCtrl.pipe();
        return;
      }
      console.log(tableState);

      $scope.isLoading = true;

      var params = {};
      angular.copy(tableState.search.predicateObject, params);

      params.pageNo = tableState.pagination.start/20 + 1 || 1;
      params.pageSize = tableState.pagination.number || PAGE_SIZE;
      console.log(params);
      hotelManageService.companyList(params).then(function (data) {
        $scope.data = data.data;
        tableState.pagination.numberOfPages = data.data.totalPage;
        $scope.isLoading = false;
      });
    };

    $scope.addUpdateItem = function (item) {
      var formData = {};
      angular.copy(item, formData);
      var modalInstance = $uibModal.open({
        size: 'lg',
        animation: true,
        templateUrl: 'app/pages/staffManage/companyList/companyList/modal/setCompany.html',
        controller: 'SetCompanyModalCtrl',
        resolve: {
          item: function () {
            return item ? formData : item
          },

        }
      });

      modalInstance.result.then(function (modalItem) {
        var postData = {};
        angular.copy(modalItem, postData);
        (item ? hotelManageService.updateCompany(postData, item.id) : hotelManageService.addCompany(postData)).then(function (data) {
          if (data && data.errcode === 0) {
            toastr.success('操作成功', '成功!');
            $scope.callServer();
          }
        });

      });
    };

    $scope.deleteItem = function (data) {
      SweetAlert.swal({
          title: "确定?",
          text: "您将要" + (data.delFlag == 1 ? '启用' : '禁用') + "此公司。",
          showCancelButton: true,
          confirmButtonColor: "#DD6B55",
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          closeOnConfirm: true},
        function(isConfirm) {
          if (isConfirm) {
            hotelManageService.deleteCompany(data.id, data.delFlag == 1 ? 0 : 1).then(function (data) {
              if (data && data.errcode === 0) {
                toastr.success('操作成功', '成功!');
                $scope.callServer();
              }
            })
          }
        });
    };


  }
})();
